#include "filepathmanager.h"
#include <QApplication>
#include <QFileInfo>
#include <QDir>

static bool fileExist(const QString &path)
{
    return QFileInfo(path).isFile();
}

static bool dirExist(const QString &path)
{
    return QFileInfo(path).isDir();
}

FilePathManager::FilePathManager()
{
    QString dataDir = rootDir();
    QString datapath = rootDir() + "datapath.txt";

    if (fileExist(datapath))
    {
        QFile file(datapath);
        if (file.open(QFile::ReadOnly))
        {
            QString dirpath = file.readAll().trimmed();
            dirpath = dirpath.replace("\\", "/");
            if (dirExist(dirpath) && !dirpath.endsWith("/"))
            {
                dataDir = dirpath + "/";
            }

            file.close();
        }
    }

    m_dataDir = dataDir;
}

FilePathManager *FilePathManager::instance()
{
    static FilePathManager fpm;
    return &fpm;
}

QString FilePathManager::rootDir()
{
    return qApp->applicationDirPath() + "/";
}

QString FilePathManager::configFile()
{
    return m_dataDir + "config.ini";
}

QString FilePathManager::verFile()
{
    return m_dataDir + "ver.txt";
}

QString FilePathManager::dbPath()
{
    return m_dataDir + "data/M1.db";
}

QString FilePathManager::reportDir()
{
    return m_dataDir + "Report/";
}

QString FilePathManager::logDir()
{
    return m_dataDir + "logs/";
}

QString FilePathManager::introductionDir()
{
    return rootDir() + "doc/EBXP-H1_Introduction/";
}

QString FilePathManager::manualFile()
{
    return rootDir() + "doc/manual.pdf";
}

QString FilePathManager::styleSheetCss()
{
    return ":/resources/etta.css";
}

QString FilePathManager::normalBtnEnableCss()
{
    return "border:none;border-image:url(:/resources/config_enable.png);color:#FFFFFF;";
}

QString FilePathManager::normalBtnDisEnableCss()
{
    return "border:none;border-image:url(:/resources/config_disable.png);color:#000000";
}

QString FilePathManager::upgradeScript()
{
    return rootDir() + "scripts/upgrade.sh";
}

QString FilePathManager::versionFile()
{
    return rootDir() + "scripts/version.txt";
}

QString FilePathManager::qmFile(const QString &lang)
{
    return QString(":/translations/etta_%1.qm").arg(lang.toLower());
}

QString FilePathManager::chartDir()
{
    return m_dataDir + "data";
}

QString FilePathManager::rawDataDir()
{
    return m_dataDir + "data/";
}

QString FilePathManager::audioDir()
{
    return rootDir() + "audio/";
}

QString FilePathManager::usbMountDir()
{
    return m_dataDir + "../usb/";
}

QString FilePathManager::usbRootDir()
{
    return usbMountDir() + "Ettadata/";
}

QString FilePathManager::usbRawDataDir()
{
    return usbRootDir() + "Rawdata/";
}

QString FilePathManager::usbRawLogDir()
{
    return usbRootDir() + "Rawlog/";
}
